//
//  CustomNavigationAnimationController.m
//  Nav
//
//  Created by Alimjan on 15/10/21.
//  Copyright © 2015年 Alimjan. All rights reserved.
//

#import "CustomNavigationAnimationController.h"
#define ALPHA 0.6
@implementation CustomNavigationAnimationController
- (NSTimeInterval)transitionDuration:
(id<UIViewControllerContextTransitioning>)transitionContext {
    return 0.5f;
}

- (void)animateTransition:
(id<UIViewControllerContextTransitioning>)transitionContext {
    if (self.reverse == NO) {
        // 1. Get controllers from transition context
        //显示第二个的动画
        UIViewController *fromVC = [transitionContext
                                    viewControllerForKey:UITransitionContextFromViewControllerKey];
        UIViewController *toVC = [transitionContext
                                  viewControllerForKey:UITransitionContextToViewControllerKey];
        
        // 2. Set init frame for toVC
        CGRect screenBounds = [[UIScreen mainScreen] bounds];
        CGRect finalFrame = [transitionContext finalFrameForViewController:toVC];
        toVC.view.frame = CGRectOffset(finalFrame, -screenBounds.size.width, 0);
        // 3. Add toVC's view to containerView
        UIView *containerView = [transitionContext containerView];
        [containerView addSubview:toVC.view];
        
        // 4. Do animate now
        NSTimeInterval duration = [self transitionDuration:transitionContext];
        [UIView animateWithDuration:duration animations:^{
            toVC.view.frame = finalFrame;
            fromVC.view.frame = CGRectOffset([transitionContext initialFrameForViewController:fromVC],screenBounds.size.width/2.0,0);
            fromVC.view.alpha = ALPHA;
            toVC.view.alpha  = 1.0;
        } completion:^(BOOL finished) {
            [transitionContext completeTransition:YES];
        }];
    } else {
        // 1. Get controllers from transition context
        UIViewController *fromVC = [transitionContext
                                    viewControllerForKey:UITransitionContextFromViewControllerKey];
        UIViewController *toVC = [transitionContext
                                  viewControllerForKey:UITransitionContextToViewControllerKey];
        
        // 2. Set init frame for fromVC
        CGRect screenBounds = [[UIScreen mainScreen] bounds];
        CGRect initFrame = [transitionContext initialFrameForViewController:fromVC];
        CGRect finalFrame = CGRectOffset(initFrame, -screenBounds.size.width, 0);
        
        
        // 3. Add target view to the container, and move it to back.
        UIView *containerView = [transitionContext containerView];
        [containerView addSubview:toVC.view];
        [containerView sendSubviewToBack:toVC.view];
        
        // 4. Do animate now
        NSTimeInterval duration = [self transitionDuration:transitionContext];
        [UIView animateWithDuration:duration
                         animations:^{
                             fromVC.view.frame = finalFrame;
                             toVC.view.frame =initFrame;
                             
                             toVC.view.alpha = 1.0;
                             fromVC.view.alpha  = 1.0;
                         }
                         completion:^(BOOL finished) {
                             [transitionContext
                              completeTransition:![transitionContext transitionWasCancelled]];
                         }];
    }
}
@end
